package game.module.gm.logic;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.common.AwardConstants;
import game.entity.PlayingRole;
import game.module.award.logic.AwardUtil;
import game.module.award.logic.ModuleConstants;
import game.module.club.logic.ClubManager;
import game.module.dabt.logic.PayMoneyBack;
import game.module.item.logic.ItemManager;
import game.module.mail.logic.MailManager;
import game.module.user.logic.PlayerDaoHelper;
import lua.LuaMessageHall.C2SClubCreate;
import lua.LuaMessageHall.S2CClubCreate;
import lua.LuaMessageHall.S2CGmCmd;

/**
 * @author: Pomelo.Lee
 * @date: 2017年8月25日 下午2:45:07
 */
public class GmCmdManager {

	private static Logger logger = LoggerFactory.getLogger(GmCmdManager.class);

	static class SingletonHolder {
		static GmCmdManager instance = new GmCmdManager();
	}

	public static GmCmdManager getInstance() {
		return SingletonHolder.instance;
	}

	public void jindou(PlayingRole hero,String[] cmdStrList, S2CGmCmd respMsg) {
		if (cmdStrList.length < 2) {
			logger.error("jindou command too short");
			respMsg.ret = 10;
			hero.getGamePlayer().writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		final int diamondVal = Integer.valueOf(cmdStrList[1]);
		long newtemp = PayMoneyBack.getInstance().payMoneybackonline(hero.getId(), true, diamondVal);
		PlayerDaoHelper.getInstance().asyncAddJinDouForPlayerBean(hero.getId(),newtemp);
		AwardUtil.send(hero, AwardConstants.JIN_DOU, (int)newtemp, ModuleConstants.MODULE_GM);
		hero.getGamePlayer().writeAndFlush(respMsg.build(hero.alloc()));
	}
	public void fangKa(PlayingRole hero, String[] cmdStrList, S2CGmCmd respMsg) {
		if (cmdStrList.length < 2) {
			logger.error("fangka command too short");
			respMsg.ret = 10;
			hero.getGamePlayer().writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		final int diamondVal = Integer.valueOf(cmdStrList[1]);
		PlayerDaoHelper.getInstance().asyncAddFangKa(diamondVal, hero.getId());
		AwardUtil.send(hero, AwardConstants.FANG_KA, diamondVal, ModuleConstants.MODULE_GM);
		hero.getGamePlayer().writeAndFlush(respMsg.build(hero.alloc()));
	}

	public void addItem(PlayingRole hero, String[] cmdStrList, S2CGmCmd respMsg) {
		if (cmdStrList.length < 3) {
			logger.error("item command too short");
			respMsg.ret = 10;
			hero.getGamePlayer().writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		int itemTplId = Integer.valueOf(cmdStrList[1]);
		int itemCount = Integer.valueOf(cmdStrList[2]);
		boolean itemExist = ItemManager.getInstance().checkTemplateExist(itemTplId);
		if (!itemExist) {
			logger.error("道具不存在！,itemTplId={}", itemTplId);
			respMsg.ret = 2;
			hero.getGamePlayer().writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		if (itemCount <= 0 || itemCount > 10000) {
			logger.error("道具数量不对！,itemCount={}", itemCount);
			respMsg.ret = 3;
			hero.getGamePlayer().writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		ItemManager.getInstance().addItem(hero, itemTplId, itemCount, hero.getId());
		respMsg.ret = 0;
		hero.getGamePlayer().writeAndFlush(respMsg.build(hero.alloc()));
	}

	public void addMail(PlayingRole hero, String[] cmdStrList, S2CGmCmd respMsg) {
		if (cmdStrList.length < 2) {
			logger.error("mail command too short");
			respMsg.ret = 10;
			hero.getGamePlayer().writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		int templateId = Integer.valueOf(cmdStrList[1]);
		MailManager.getInstance().addMail(hero.getId(), templateId);
		respMsg.ret = 0;
		hero.getGamePlayer().writeAndFlush(respMsg.build(hero.alloc()));
	}

	public void createClub(PlayingRole hero, String[] cmdStrList, S2CGmCmd respMsg) {
		if (cmdStrList.length < 4) {
			logger.error("mail command too short");
			respMsg.ret = 10;
			hero.getGamePlayer().writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		C2SClubCreate clubReq = new C2SClubCreate();
		clubReq.clubname = cmdStrList[1];
		clubReq.phone = cmdStrList[2];
		clubReq.code = cmdStrList[3];
		S2CClubCreate cResp = new S2CClubCreate();
		ClubManager.getInstance().createClub(hero, clubReq, cResp);
	}

}
